package com.shakespace.artofandroid.chapter12bitmapandcache

import android.os.Bundle
import android.widget.AbsListView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.shakespace.artofandroid.R
import kotlinx.android.synthetic.main.activity_gallery.*
import java.lang.RuntimeException


class GalleryActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gallery)
        ActivityCompat.requestPermissions(
            this, arrayOf(
                android
                    .Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 1
        )
        val data = mutableListOf<String>(
            "http://pic-bucket.ws.126.net/photo/0003/2021-04-13/G7ETR3ID00AJ0003NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0003/2021-04-13/G7ETR3IE00AJ0003NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0003/2021-04-13/G7ETR3IF00AJ0003NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0003/2021-04-13/G7ETR3IG00AJ0003NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0003/2021-04-13/G7ETR3IH00AJ0003NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31R800A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31R900A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RA00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RB00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RC00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RD00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RE00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0026/2021-04-10/G78P31RF00A70026NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-02-19/G36HPBJH00AO0001NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-02-19/G36HPBJJ00AO0001NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-04-10/G77B1B3700AO0001NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-04-10/G77B1B3800AO0001NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-04-10/G77B1B3900AO0001NOS.jpg",
            "http://pic-bucket.ws.126.net/photo/0001/2021-04-10/G77B1B3A00AO0001NOS.jpg"
        )
        val galleyAdapter = GalleyAdapter(this, data)

        // 仅在停止滑动时加载
        grid_view.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    galleyAdapter.setIsGridViewIdle(true);
                    galleyAdapter.notifyDataSetChanged();
                } else {
                    galleyAdapter.setIsGridViewIdle(false);
                }
            }

            override fun onScroll(
                view: AbsListView?,
                firstVisibleItem: Int,
                visibleItemCount: Int,
                totalItemCount: Int
            ) {
            }

        })
        grid_view.adapter = galleyAdapter

        mock_error.setOnClickListener {
            throw RuntimeException("一个自定义的异常")
        }

    }
}